package handel

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"post/api/inits"
	"post/internal/model"
	req2 "post/internal/req"
	"post/pkg"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {
	var req req2.SendSmsReq
	c.ShouldBind(&req)

	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(strconv.Itoa(req.Mobile), strconv.Itoa(code))
	//if err != nil {
	//	return
	//}
	//if *sms.Body.Code != "OK" {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code":    500,
	//		"message": *sms.Body.Message,
	//		"data":    nil,
	//	})
	//} else {
	key := fmt.Sprintf("send%d", req.Mobile)
	inits.Rdb.Set(inits.Ctx, key, code, 0)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "发送成功",
		"data":    nil,
	})
	//}
}
func Login(c *gin.Context) {
	var req req2.Login
	c.ShouldBind(&req)

	var user model.User
	tx := inits.DB.Begin()
	tx.Where("mobile = ?", req.Mobile).Find(&user)
	now := time.Now()

	//判断短信验证码
	key := fmt.Sprintf("send%d", req.Mobile)
	code, _ := inits.Rdb.Get(inits.Ctx, key).Int()

	if code != req.Code {
		c.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "验证码错误",
			"data":    nil,
		})
		tx.Rollback()
	} else {
		if user.ID == 0 {
			c.JSON(http.StatusOK, gin.H{
				"code":    200,
				"message": "手机号不存在,将为你自动注册....",
				"data":    nil,
			})
			var users = model.User{
				Mobile:    req.Mobile,
				HeadImg:   "123.img",
				NickName:  "默认用户",
				Topic:     "无",
				Reals:     0,
				Fs:        0,
				LoginTime: now,
			}
			tx.Create(&users)
			tx.Commit()
		} else {
			tx.Table("users").Where("mobile = ?", req.Mobile).Update("login_time", now)
			handler, err := pkg.TokenHandler(int(user.ID))
			if err != nil {
				c.JSON(http.StatusOK, gin.H{
					"code":    500,
					"message": "获取token失败",
					"data":    nil,
				})
				tx.Rollback()
			}
			var u = map[string]interface{}{
				"Id":        user.ID,
				"Mobile":    user.Mobile,
				"HeadImg":   user.HeadImg,
				"NickName":  user.NickName,
				"Topic":     user.Topic,
				"Reals":     user.Reals,
				"Fs":        user.Fs,
				"LoginTime": user.LoginTime,
			}
			inits.Rdb.HMSet(inits.Ctx, "userShow"+strconv.Itoa(user.Mobile), u)
			c.JSON(http.StatusOK, gin.H{
				"code":    200,
				"message": "登录成功",
				"data":    handler,
			})
			tx.Commit()
		}
	}

}
func UserShow(c *gin.Context) {
	var req req2.UserShow
	c.ShouldBind(&req)
	var user model.User
	inits.DB.Debug().Where("id = ?", req.UserId).Find(&user)
	var u = map[string]interface{}{
		"mobile":     user.Mobile,
		"head_img":   user.HeadImg,
		"nick_name":  user.NickName,
		"topic":      user.Topic,
		"reals":      user.Reals,
		"fs":         user.ID,
		"login_time": user.LoginTime,
	}
	key := fmt.Sprintf("UserShow" + strconv.Itoa(int(user.ID)))
	inits.Rdb.HMSet(inits.Ctx, key, u)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查看成功",
		"data":    user,
	})
}
func UserUpdate(c *gin.Context) {
	var req req2.UserUpdate
	c.ShouldBind(&req)
	var user = model.User{
		Mobile:    req.Mobile,
		HeadImg:   req.HeadImg,
		NickName:  req.NickName,
		LoginTime: time.Time{},
	}
	inits.DB.Debug().Where("id = ?", req.UserId).Find(&user)
	inits.DB.Debug().Where("id = ?", req.UserId).Updates(&user)
	var u = map[string]interface{}{
		"mobile":     user.Mobile,
		"head_img":   user.HeadImg,
		"nick_name":  user.NickName,
		"topic":      user.Topic,
		"reals":      user.Reals,
		"fs":         user.ID,
		"login_time": user.LoginTime,
	}
	key := fmt.Sprintf("UserShow" + strconv.Itoa(int(user.ID)))
	if inits.Ext(key) {
		inits.Rdb.Del(inits.Ctx, key)
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "删除原有并更新",
			"data":    nil,
		})
	} else {
		inits.Rdb.HMSet(inits.Ctx, key, u)
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "修改成功",
			"data":    user,
		})
	}

}
